package com.zeroes.cons;
import java.util.*;

import org.zeroes.svg.SVGElement;

import android.util.Log;


public class ZSLayer {
	
	public String title="Layer0"; 
	public int type=ZSGame.LAYER_TYPE_NORMAL;
	protected ZSGame thisGame;
	public int width=0;
	public int height=0;
	public float centreX=0;
	public float centreY=0;
	public float rotation=0;
	public float tx=0;
	public float ty=0;
	public float lsx=1;
	public float lsy=1;
	public float anchorX=0.5f;
	public float anchorY=0.5f;
	public float opacity=1;
	public boolean batch=false;
	public List<ZSSprite> Content ;
	protected List<ZSGroup> Groups;
	protected boolean visible=true;
	public boolean OrderingDirty=false;
	public boolean SocialDirty=false;
	protected ZSRenderer _gl=ZSGameManager.AskMe().GL;
	public ZSLayer(String title, ZSGame g){
		
		this.thisGame=g;
		this.width=g.width;
		this.height=g.height;
		this.centreX=width/2;
		this.centreY=height/2;
		
		this.title=title;
		this.Groups=Collections.synchronizedList(new ArrayList<ZSGroup>(0));
		this.Groups.add(new ZSGroup("freeEntities",this));
		Content = Collections.synchronizedList(new ArrayList<ZSSprite>(0));
		
	}
	
	public void addGroup(SVGElement _g){
		ZSGroup tg = new ZSGroup(_g.id,this);
		tg.rotation=_g.rot;
		tg.gsx=_g.sx;
		tg.gsy=_g.sy;
		this.Groups.add(tg);
		
	}
	
	public void addSprite(ZSSprite spr){
	spr.setparentID("freeEntities");
	spr.setGroup(this.getGroupNamed("freeEntities"));
	Content.add(spr);
	if(!this.thisGame.sg.containsKey(spr.tag))
	this.thisGame.sg.put(spr.tag,this.title+"."+"freeEntities");
	this.SocialDirty=true;
	}

	public void addGroupedSprite(ZSSprite spr, String gt){
		spr.parentID=gt;
		spr.setGroup(this.getGroupNamed(gt));
		Content.add(spr);
		if(!this.thisGame.sg.containsKey(spr.tag))
		this.thisGame.sg.put(spr.tag,this.title+"."+gt);
		this.SocialDirty=true;
	}
	
	public ZSGroup getGroupNamed(String n){
		
		for(ZSGroup g:Groups){
			
			if(g.id.equals(n))
				return g;
		}
		return null;
	}
public ZSSprite getSpriteforID(String sid){
	
	for(ZSSprite sp:Content){
		if(sp.tag.equals(sid)){
			return sp;
		}
		
	}
	return null;
}

	public void reOrder(){
		 Collections.sort(Content, new Comparator<ZSSprite>() {

             @Override
             public int compare(ZSSprite o1, ZSSprite o2) {
                     if (o1.getZ() > o2.getZ())
                             return 1;
                     else if (o1.getZ() < o2.getZ())
                             return -1;
                     else
                             return 0;
             }
     });
	
	OrderingDirty=false;
	}
	public void visit(float alfa){
		if(!visible)
			return;
		
		 if(OrderingDirty)
			 reOrder();
		
		for(ZSGroup g:Groups){
			g.visit(alfa);
			if(g.Content!=null){
			for(ZSSprite ss:g.Content){
				//ss.marktransformDirty();
				//Log.v("gtrans", ss.tag+" visit from ZSLayer tr dirty "+ss.transformDirty);
				ss.visitLocal(alfa);
			}
			
			}
			if(!g.id.equals("freeEntities")){
			g.calculateBB();
			/*for(ZSSprite ss:g.Content){
				ss.visitGrouped(alfa);
			}*/
			}
			
		
			
		}
	/*	for(ZSSprite s:Content){
			s.visit(alfa);
		}*/
		
		
	}
	public void removeGroup(ZSGroup gr){
		if(this.Groups.contains(gr)){
			
			for(ZSSprite sp : gr.Content){
				this.removeSprite(sp);
				
			}
			this.Groups.remove(gr);
		}
		
	}
	public void DirtytoOrderingDirty(){
		
		OrderingDirty=true;
		
	}
	
	public void translate(float x,float y){
		
		tx=x*this.thisGame.getXRatio();
		ty=y*this.thisGame.getYRatio();
		this.markTransformDirty();
	}
	
	public void rotateClockwise(float deg){
		this.rotation+=deg;
		
		if(this.rotation>=360)
		{
			this.rotation-=360;
			//this.p_rotation-=360;
		}
		this.markTransformDirty();
		
	}
	public void setRotation(float rot){
		
		this.rotation=rot;
		this.markTransformDirty();
	}
	public void rotateCounterClockwise(float deg){
		this.rotation-=deg;
		
		if(this.rotation<=-360)
		{
			this.rotation+=360;

		}
		this.markTransformDirty();
		
	}
	public void setScale(float onX,float onY){
		this.lsx=onX;
		this.lsy=onY;
		this.markTransformDirty();
		
	}
	public void markTransformDirty(){
		this.height=thisGame.height;
		width=thisGame.width;
		this.centreX=(width*anchorX)+tx;
		this.centreY=(height*anchorY)+ty;

		
		for(ZSSprite s:Content){
			s.marktransformDirty();
			//s.marktransformGroupDirty();
		}
		for(ZSGroup g:Groups){
			
			g.transformDirty=true;
		}
	}
	
	public List<ZSSprite> getSpritesinGroup(String gt){
		List<ZSSprite> ret = new ArrayList();
		for(ZSSprite s:Content){
			if(s.parentID.equals(gt)){
				ret.add(s);
				
			}
			
		}
		
		return ret;
	}
	
	public void moveSpriteintoGroup(ZSSprite spr,String groupTitle){
		
		spr.parentID=groupTitle;
		spr.setGroup(this.getGroupNamed(groupTitle));
		String w=thisGame.sg.get(spr.tag);
		String[] items = w.split("\\.");
		thisGame.sg.put(spr.tag, items[0]+"."+groupTitle);
		
		this.SocialDirty=true;
	}
	
	public  void moveSpriteintoGroupAndLayer(ZSSprite spr,String groupTitle,String layerTitle){
		boolean canMove=true;
		for(ZSLayer layer:thisGame.Layers){
			if(layer.title.equals(layerTitle)&&layer.type==ZSGame.LAYER_TYPE_ATLAS){
				canMove=false;
			}
				
		}
		if(canMove){
			
		
		ZSLayer tgl=thisGame.getLayerByTitle(layerTitle);
		if(Content.contains(spr)){
		
		spr.parentID=groupTitle;
		spr.setGroup(tgl.getGroupNamed(groupTitle));
		
		this.Content.remove(spr);
		
		
		tgl.Content.add(spr);
		thisGame.sg.put(spr.tag, layerTitle+"."+groupTitle);
		}
		this.SocialDirty=true;
		
		tgl.SocialDirty=true;
		}
	}
	
	public boolean removeSprite(ZSSprite s){
		
		if(Content.contains(s)){
			
			Content.remove(s);
		//	thisGame.sg.remove(s.tag);
			this.SocialDirty=true;
			return true;
		}
		return false;
	}
	
	public void savepreupdateValues(){
		for(ZSSprite s:Content){
			s.savepreupdateValues();
		}
		
	}
	
	public void hide(){
		
		if(Content!=null){
		this.visible=false;
			for(ZSSprite s:Content){
				s.hide();
		
			}
			}
	}
	
	public void show(){
		
		if(Content!=null){
		this.visible=true;
			for(ZSSprite s:Content){
				s.show();
		
			}
			}
	}
	public void setOpacity(float o){
		if(o>1){
			this.opacity=1;
		}
		else{
			if(o<0) 
				this.opacity=0;
			else
				this.opacity=o;
		}
		if(Content!=null){

			for(ZSSprite s:Content){
				s.opacity=this.opacity;
		
			}
			}
		
	}
	public void removeAllSprites(){
	
	
		/*for(ZSSprite s:Content){
			Log.v("gtrans", s.tag+" spera sa ma curate ");
		
			this.removeSprite(s);
	
		}*/
	}
}
